
local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseComponent(),script.Parent.Name);
local UIManager = ClientOHOH.load.UIManager();
local CallBackManager = ClientOHOH.load.CallBackManager();
local FunctionList = ClientOHOH.load.FunctionList();

local SettingGui=UIManager:CreateUI("SettingGui",require(script.SettingGui))



local LanguageHelper= _G.LanguageHelper

local SettingKeyList={
	{key="bgm",name=23},
	{key="audio",name=24},
	-- {key="rebirth",name=131}

}

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.data =self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent()):GetData("set_data");
	self.btns={}
	for _,info in pairs(SettingKeyList) do
		local btn= SettingGui:AddSetBtn(info.key,LanguageHelper:GetText(info.name),self.data[info.key]==1)
		self.btns[info.key]=btn
		
		CallBackManager:AddRbxConnect(self, btn.Activated, function()
			-- if self:ChangeSettings(info.key) then
			-- 	local fucId="set_"..info.key
			-- 	if M[fucId] then
			-- 		M[fucId](self,self.data[info.key])
			-- 	end
			-- 	SettingGui:SetBtnSwitch2(btn,self.data[info.key]==1)
			-- end
			self:Setting(info.key)
		end)

		local fucId="set_"..info.key
		if M[fucId] then
			M[fucId](self,self.data[info.key])
		end
	end
	SettingGui:InitAllButtons()


	CallBackManager:AddRbxConnect(self, SettingGui.insts.codeBtn.Activated, function()

		self:CodeExchange(SettingGui:GetCode())
	end)

end;


function M.CodeExchange(self,code)

	if not code then
		return
	end
	--去除回车换行
	code = string.gsub(code, "\r\n", "")
	--去除空格
	code = string.gsub(code, "%s+", "")
	--转为大写
	code = string.upper(code)

	if code =="" then
		return
	end

	local reward=ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.code_exchange,code)
	if reward then
		self:AddProcess(function()
			for k,v in pairs(reward) do
				local image,isImage=_G.CommonOHOH.load.ItemTool().GetItemShowView(k)
				if image and isImage then
					ClientOHOH.load.UIManager().ui_list.GlobalTipGui:ShowTextAndImage("+"..v,image)
					self:Wait(0.5)
				end
			end
		end)
		ClientOHOH.load.Client_AudioManager():PlayGloble("claim")
	else
		ClientOHOH.load.UIManager().ui_list.GlobalTipGui:Show(LanguageHelper:GetText(22) )
	end
end

function M.dtor(self)
	M.super.dtor(self);
end;

local Client_AudioManager=ClientOHOH.load.Client_AudioManager()
local AudioGroup=ClientOHOH.config.AudioGroup


function M.set_bgm(self,v)

	Client_AudioManager:SetGroupVolume("bgm",AudioGroup.bgm*v)
end

function M.set_audio(self,v)
	Client_AudioManager:SetGroupVolume("ui",AudioGroup.ui*v)
	Client_AudioManager:SetGroupVolume("effect",AudioGroup.effect*v)
end

function M.set_showPet(self,v)
	-- if self:ChangeSettings(info.key) then
	-- 	local fucId="set_"..info.key
	-- 	if M[fucId] then
	-- 		M[fucId](self,self.data[info.key])
	-- 	end
	-- 	SettingGui:SetBtnSwitch2(btn,self.data[info.key]==1)
	-- end
end

function M.set_rebirth(self,v)
end

function M.Setting(self,key)
	local btn=self.btns[key]
	if self:ChangeSettings(key) then
		local fucId="set_"..key
		if M[fucId] then
			M[fucId](self,self.data[key])
		end
		SettingGui:SetBtnSwitch2(btn,self.data[key]==1)
	end
end
function M.ChangeSettings(self,key,v)
	if self.parent:Invoke(FunctionList.set_change,key,v) then
		if self.data[key]==0 then
			self.data[key]=1
		else
			self.data[key]=0
		end
		return true
	end
	return false
end

return M